package com.huawei.esdk.tp.device.smc.v100r003c00;

import java.util.Date;
import java.util.List;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.commu.itf.ICXFSOAPProtocolAdapter;
import com.huawei.esdk.platform.exception.ProtocolAdapterException;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.device.smc.v100r002c01.SMC20V1R2C01ConferenceManagerCapabilityImpl;
import com.huawei.esdk.tp.device.smc.v100r003c00.SMC20V1R3C00IConferenceManagerCapability;
import com.huawei.esdk.tp.device.smc.v100r003c00.convert.TPSMC20V1R3C00ConfMgrConvert;
import com.huawei.esdk.tp.domain.model.Conference;
import com.huawei.esdk.tp.domain.model.PeriodicalConference;
import com.huawei.esdk.tp.domain.model.ScheduleConference;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.SiteInConference;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.AddSiteInScheduledConf;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.AddSiteInScheduledConfResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.EditFutureConf;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.EditFutureConfResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.ScheduleConf;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.ScheduleConfResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.QueryConfSitesStatus;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.QueryConfSitesStatusResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.QueryConferencesStatus;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.QueryConferencesStatusResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.SmcExternal2Soap;

public class SMC20V1R3C00ConferenceManagerCapabilityImpl extends SMC20V1R2C01ConferenceManagerCapabilityImpl implements
    SMC20V1R3C00IConferenceManagerCapability
{
    private TPSMC20V1R3C00ConfMgrConvert tpConfMgrConvertService = new TPSMC20V1R3C00ConfMgrConvert();
    
    /** * @param cxfSoapProtocolAdapter */
    public SMC20V1R3C00ConferenceManagerCapabilityImpl(ICXFSOAPProtocolAdapter cxfSOAPProtocolAdapter)
    {
        super(cxfSOAPProtocolAdapter);
    }
    
    protected String getR3SouthItfName()
    {
        return SmcExternal2Soap.class.getName();
    }
    
    @Override
    public SDKResult<List<SiteInConference>> queryConfSitesStatus(String confId, List<String> siteUris)
    {
        // modelbean 转南向请求对象
        QueryConfSitesStatus reqBean = tpConfMgrConvertService.queryConfSitesStatusModel2Soap(confId, siteUris);
        
        // 南向产品bean的名称：QueryConfSitesStatus
        QueryConfSitesStatusResponse res = null;
        SDKResult<List<SiteInConference>> result = new SDKResult<List<SiteInConference>>();
        try
        {
            res =
                (QueryConfSitesStatusResponse)sendProtocolMessage(reqBean, getR3SouthItfName(), "queryConfSitesStatus");
            result = tpConfMgrConvertService.queryConfSiteStatusSoap2model(res);
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        
        return result;
    }
    
    @Override
    public SDKResult<List<Conference>> queryConfStatus(List<String> confIds)
    {
        SDKResult<List<Conference>> result = new SDKResult<List<Conference>>();
        QueryConferencesStatus south = tpConfMgrConvertService.list2QueryConferencesStatusSouth(confIds);
        QueryConferencesStatusResponse response = null;
        try
        {
            response =
                (QueryConferencesStatusResponse)sendProtocolMessage(south,
                    getR3SouthItfName(),
                    "queryConferencesStatus");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        
        if (null == response)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result = tpConfMgrConvertService.queryConferencesStatusResponseSouth2SDKResult(response);
        }
        return result;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public SDKResult<ScheduleConference> scheduleConference(ScheduleConference conf)
    {
        
        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        ScheduleConfResponse responseSouth = null;
        try
        {
            ScheduleConf resMsg = tpConfMgrConvertService.scheduleConfModel2soap(conf);
            
            responseSouth = (ScheduleConfResponse)sendProtocolMessage(resMsg, getR3SouthItfName(), "scheduleConf");
            result = (SDKResult<ScheduleConference>)tpConfMgrConvertService.scheduleConfSoap2model(responseSouth);
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        catch (SDKException e)
        {
            result.setErrCode(e.getSdkErrCode());
            return result;
        }
        
        return result;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public SDKResult<PeriodicalConference> scheduleRecurrenceConference(PeriodicalConference conf)
    {
        SDKResult<PeriodicalConference> result = new SDKResult<PeriodicalConference>();
        ScheduleConfResponse responseSouth = null;
        try
        {
            ScheduleConf resMsg = tpConfMgrConvertService.scheduleConfModel2soap(conf);
            
            responseSouth = (ScheduleConfResponse)sendProtocolMessage(resMsg, getR3SouthItfName(), "scheduleConf");
            result = (SDKResult<PeriodicalConference>)tpConfMgrConvertService.scheduleConfSoap2model(responseSouth);
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        catch (SDKException e)
        {
            result.setErrCode(e.getSdkErrCode());
            return result;
        }
        
        return result;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public SDKResult<ScheduleConference> editScheduleConference(ScheduleConference conf)
    {
        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        EditFutureConfResponse responseSouth = null;
        try
        {
            EditFutureConf resMsg = tpConfMgrConvertService.editScheduleConfModel2soap(conf);
            
            responseSouth = (EditFutureConfResponse)sendProtocolMessage(resMsg, getR3SouthItfName(), "editFutureConf");
            result = (SDKResult<ScheduleConference>)tpConfMgrConvertService.editScheduleConfSoap2model(responseSouth);
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        catch (SDKException e)
        {
            result.setErrCode(e.getSdkErrCode());
            return result;
        }
        
        return result;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public SDKResult<PeriodicalConference> editScheduleRecurrenceConference(PeriodicalConference conf)
    {
        SDKResult<PeriodicalConference> result = new SDKResult<PeriodicalConference>();
        EditFutureConfResponse responseSouth = null;
        try
        {
            EditFutureConf resMsg = tpConfMgrConvertService.editScheduleConfModel2soap(conf);
            
            responseSouth = (EditFutureConfResponse)sendProtocolMessage(resMsg, getR3SouthItfName(), "editFutureConf");
            result = (SDKResult<PeriodicalConference>)tpConfMgrConvertService.editScheduleConfSoap2model(responseSouth);
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }
        catch (SDKException e)
        {
            result.setErrCode(e.getSdkErrCode());
            return result;
        }
        
        return result;
    }
    
    @Override
    public SDKResult<ScheduleConference> addSite(String confId, Site site, Date beginTime, boolean useAnonymousSite)
    {
        SDKResult<ScheduleConference> modelBean = new SDKResult<ScheduleConference>();
        
        // 1.领域Bean转成南向SOAP Bean
        ScheduleConference conference = new ScheduleConference(confId);
        
        // 2.向SMC发SOAP消息
        AddSiteInScheduledConfResponse southResponse = null;
        try
        {
            AddSiteInScheduledConf southSoap =
                tpConfMgrConvertService.addSiteToScheduledConfModel2SouthSoap(conference,
                    site,
                    beginTime,
                    useAnonymousSite);
            
            southResponse =
                (AddSiteInScheduledConfResponse)cxfSOAPProtocolAdapter.syncSendMessageWithCxf(southSoap,
                    getR3SouthItfName(),
                    "addSiteInScheduledConf");
        }
        catch (ProtocolAdapterException e)
        {
            // 打日志
            LOGGER.error("addSite method error: " + e);
            modelBean.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            return modelBean;
        }
        catch (SDKException e)
        {
            modelBean.setErrCode(e.getSdkErrCode());
            return modelBean;
        }
        
        // 3.将SMC返回的南向SOAP Bean转成领域Bean
        modelBean = tpConfMgrConvertService.addSiteToScheduledConfSouthSoap2Model(conference, site, southResponse);
        
        return modelBean;
    }
}
